﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BuildPlannerLib.Enums;
using BuildPlannerLib.PlayerActions;
using BuildPlannerLib.SimEvents;
using BuildPlannerLib.Terran.Buildings;

namespace BuildPlannerLib.Terran.PlayerActions
{
    // Derived class from Action for a Terran player expanding to a new base
    public class TerranExpand : PlayerAction
    {
        // Field for the base from which the SCV will be taken and the Command Center will be placed
        public Base WorkerBase;
        public int Base;

        // Constructor with parameters for base and build
        public TerranExpand(Build build, Base workerBase, int b)
        {
            Build = build;
            WorkerBase = workerBase;
            Base = b;
            MineralCost = 400;
            TimeDuration = 10000;
            SupplyProduced = 11;
            Description = "Expansion Command Center (Base " + Base + ")";
            LongDescription = "Creating Expansion Command Center at base number " + Base + ".";
        }

        // Evaluates whether the player is Terran and whether there is an SCV to build
        public override bool RequirementsFilled()
        {
            Error = null;

            if (Build.Race != Race.Terran)
            {
                Error = "This building can only be built by Terran players.";
                return false;
            }

            if (WorkerBase.MineralWorkers == 0)
            {
                Error = "No workers assigned to minerals at the specified base.";
                return false;
            }

            if (Build.Map.Bases.ElementAt(Base).Status != BaseStatus.NotTaken)
            {
                Error = "This base has already been expanded to.";
                return false;
            }

            return base.RequirementsFilled();
        }

        // Evaluates if enough minerals are available and an SCV is available to build the structure
        public override bool StartCheck()
        {
            if (WorkerBase.MineralWorkerStatus.Sum() == 0)
                return false;

            return base.StartCheck();
        }

        // Spends the minerals, removes a worker to build the building, starts taking the base, and adds the event for finishing the action
        public override void StartAction()
        {
            CommandCenter commandCenter = Build.Player.MainBuilding as CommandCenter;

            base.StartAction();

            WorkerBase.RemoveMineralWorker(ref Build);
            Build.Player.IdleWorkers++;
            Build.Map.Bases.ElementAt(Base).StartExpansion();
            commandCenter.Started++;
        }

        // Adds the Command Center, finishes taking the base and adds the worker to the new base
        public override void FinishAction()
        {
            CommandCenter commandCenter = Build.Player.MainBuilding as CommandCenter;

            base.FinishAction();

            commandCenter.AddBuilding(Base);
            Build.Map.Bases.ElementAt(Base).FinishExpansion();
            Build.Player.IdleWorkers--;
            Build.Map.Bases.ElementAt(Base).AddMineralWorker(ref Build);
        }
    }
}
